package com.cgwx.tcservice.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.cgwx.tcservice.entity.tc.TcDirectFrameConfig;
import com.cgwx.tcservice.entity.vo.TcDirectFrameExcelVo;
import com.cgwx.tcservice.service.TcDirectFrameConfigService;
import io.swagger.annotations.Api;
import org.springframework.beans.BeanUtils;

/**
 * @author GaoWei
 * @Date 2023/8/8 - 16:25
 */

@Api(tags = "EasyExcel遥控表格读取监听器")
public class TcDirectFrameExcelListener extends AnalysisEventListener<TcDirectFrameExcelVo> {
//    不可以交给Spring进行管理，需要自己new，不能注入其他对象
    public TcDirectFrameConfigService tcDirectFrameConfigService;

//    构造函数
    public TcDirectFrameExcelListener() {
    }

    public TcDirectFrameExcelListener(TcDirectFrameConfigService tcDirectFrameConfigService) {
        this.tcDirectFrameConfigService = tcDirectFrameConfigService;
    }

    //    读取Excel中的内容
    @Override
    public void invoke(TcDirectFrameExcelVo tcDirectFrameExcelVo, AnalysisContext analysisContext) {
//        创建对象，用于数据库存储
        TcDirectFrameConfig tcDirectFrameConfig = new TcDirectFrameConfig();
//        赋值遥控指令内容
        BeanUtils.copyProperties(tcDirectFrameExcelVo,tcDirectFrameConfig);
//            存储
        tcDirectFrameConfigService.save(tcDirectFrameConfig);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }
}
